<?php

namespace App\Admin\Controllers;

use App\Models\Classroom;
use App\Models\Attendance_cycle;
use App\Models\Student;
use App\Models\Student_attendance;
use App\Models\Squad;
use App\Models\Crouse_time;
use Encore\Admin\Auth\Database\Administrator;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class Student_attendanceController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.student_attendance'));
            $content->description(trans('admin.list'));

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.student_attendance'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.student_attendance'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Student_attendance::class, function (Grid $grid) {
            $student_id = request('student_id');
            // if ($student_id = request('student_id')) {
            //     $grid->model()->ofPost($student_id);
            // }
            $grid->model()->where('student_id', '=', $student_id);
            $grid->model()->orderBy('id', 'desc');
            $grid->id(trans('admin.ID'))->sortable();
            //$grid->attendance_cycle(trans('admin.attendance_cycle'))->name();
            $grid->attendance_cycle_id(trans('admin.attendance_cycle'))->display(function($attendance_cycle_id){
                return Attendance_cycle::find($this->attendance_cycle_id)->name;
            });
            $grid->class_period(trans('admin.class_period'));
            
            $grid->attendance_start_date(trans('admin.attendance_start_date'))->sortable();
                 $grid->attendance_end_date(trans('admin.attendance_end_date'))->sortable();
            //$grid->classroom()->name();
            $grid->classroom_id(trans('admin.classroom'))->display(function($classroom_id){
                return Classroom::find($this->classroom_id)->name;
            });
            $grid->attendance_status(trans('admin.status'))->display(function ($attendance_status) {
                if($this->attendance_status==1){
                    return '正常';
                }elseif ($this->attendance_status==2) {
                    return '请假';
                }elseif ($this->attendance_status==3) {
                    return '迟到';
                }elseif ($this->attendance_status==4) {
                    return '缺勤';
                }
            })->sortable();
            //$grid->attendance_status(trans('admin.status'))->editable('select', ['1' => '正常', '2' => '迟到','3' => '请假', '4' => '缺勤'])->sortable();

            $grid->actions(function ($actions) {
                $actions->disableView();
            });

            //禁用导出按钮
            //$grid->disableExport();
            $filename="考勤记录";
            $grid->exporter(new ExcelExpoter($grid,$filename));
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->equal('attendance_cycle_id', trans('admin.attendance_cycle_id'))->select(Attendance_cycle::where('status','=','0')->get()->pluck('name', 'id'));
                    $filter->equal('attendance_status', trans('admin.attendance_status'))->radio([
                        '1'     => '正常',
                        '2'     => '请假',
                        '3'     => '迟到',
                        '4'     => '缺勤'
                    ]);
                });
                $filter->column(1/2, function ($filter) {
                    $filter->equal('class_period',trans('admin.class_period'))->integer();
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    { 
        $form = new Form(new Student_attendance());

        $form->display('id', 'ID');

        $form->select('attendance_cycle_id', '考勤周期')->options(Attendance_cycle::where('status','=','0')->get()->pluck('name', 'id'))->rules('required')->addElementLabelClass('must-input');
        $form->number('class_period', '考勤课时')->max(20)->min(1)->addElementLabelClass('must-input');
        $form->datetimeRange('attendance_start_date', 'attendance_end_date', '考勤时间段')->rules('required')->addElementLabelClass('must-input');
        $form->select('classroom_id', trans('admin.classroom'))->options(Classroom::all()->pluck('name', 'id'))->rules('required')->addElementLabelClass('must-input');
        $form->select('attendance_status',trans('admin.attendance_status'))->options([
                '1' => '正常',
                '2' => '请假',
                '3' => '迟到',
                '4' => '缺勤'
            ])->rules('required')->addElementLabelClass('must-input');
        $form->hidden('student_id')->default(request('student_id'));
            
        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->saved(function (Form $form) {
            $student_id = $form->model()->student_id;
            // 跳转页面
            return redirect('/admin/attendance_entering/'.$student_id);
            });
        return $form;
    }

    public function attendance_data_make()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('admin.student_attendance'));
            $content->description(trans('admin.create'));

            $content->body($this->attendance_enter_form());
        });
    }

    protected function attendance_grid()
    {
        return Admin::grid(Student_attendance::class, function (Grid $grid) {
            $grid->setResource('/admin/student_attendance');
            $squad_id = request('squad_id');
            $attendance_cycle_id = request('attendance_cycle_id');
            $attendance_date = request('attendance_date');
            $crouse_time_id = request('crouse_time_id');
            //$attendance_end_date = request('attendance_end_date');
            $classroom_id = request('classroom_id');
            //$attendance_status = request('attendance_status');
            $where = [
                'squad_id' =>$squad_id,
                'attendance_cycle_id' =>$attendance_cycle_id,
                'attendance_date' =>$attendance_date,
                'crouse_time_id' =>$crouse_time_id,
                'classroom_id' =>$classroom_id
            ];
            $grid->model()->where($where);
           
            //$grid->model()->orderBy('id', 'desc');
            $grid->id(trans('admin.ID'))->sortable();
            $grid->squad_id(trans('admin.squad_id'))->display(function($squad_id){
                return Squad::find($this->squad_id)->name;
            });
            $grid->student_id(trans('admin.en_name'))->display(function($student_id){
                return Student::find($this->student_id)->en_name;
            });
            $grid->attendance_cycle_id(trans('admin.attendance_cycle_id'))->display(function($attendance_cycle_id){
                return Attendance_cycle::find($this->attendance_cycle_id)->name;
            });
            $grid->attendance_date(trans('admin.attendance_date'))->sortable();
            $grid->crouse_time_id(trans('admin.crouse_time_id'))->display(function($crouse_time_id){
                return Crouse_time::find($this->crouse_time_id)->name;
            });
            
            
            //$grid->attendance_end_date(trans('admin.attendance_end_date'))->sortable();
            $grid->classroom_id(trans('admin.classroom'))->display(function($classroom_id){
                return Classroom::find($this->classroom_id)->name;
            });
            $grid->attendance_status(trans('admin.status'))->editable('select', ['1' => '正常', '2' => '请假','3' => '迟到', '4' => '缺勤'])->sortable();

            $grid->disableCreateButton();
            $grid->disableFilter();
            $grid->disableActions();

            //禁用导出按钮
            //$grid->disableExport();
            $filename="考勤记录";
            $grid->exporter(new ExcelExpoter($grid,$filename));
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
        });
    }

    protected function attendance_enter_form()
    { 
        $form = new Form(new Student_attendance());

        $form->display('id', 'ID');

        $form->select('squad_id','考勤的班级')->options(Squad::where('class_teacher','=',Admin::user()->id)->get()->pluck('name','id'));
        $form->select('attendance_cycle_id', '考勤周期')->options(Attendance_cycle::where('status','=','0')->get()->pluck('name', 'id'))->rules('required');
        $form->date('attendance_date',trans('admin.attendance_date'))->format('YYYY-MM-DD');
        $form->select('crouse_time_id',trans('admin.crouse_time_id'))->options(Crouse_time::all()->pluck('name','id'));
        //$form->number('class_period', '考勤课时')->max(20)->min(1);
        //$form->datetimeRange('attendance_start_date', 'attendance_end_date', '考勤时间段')->rules('required');
        $form->select('classroom_id', trans('admin.classroom'))->options(Classroom::all()->pluck('name', 'id'))->rules('required');
        //$form->hidden('attendance_status')->value('1');
        
            
        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->setAction('attendance_enter_save');

        return $form;
    }

    protected function attendance_enter_save()
    {
        $squad_id = request('squad_id');
        $attendance_cycle_id = request('attendance_cycle_id');
        $crouse_time_id = request('crouse_time_id');
        $attendance_date = request('attendance_date');
        //$attendance_end_date = request('attendance_end_date');
        $classroom_id = request('classroom_id');
        $attendance_status = request('attendance_status');
        $student_ids = Student::where('squad_id','=',$squad_id)->pluck('id')->toArray();
        $data = [];
        for ($i=0; $i <count($student_ids) ; $i++) { 
            $data[] = [
                'squad_id' =>$squad_id,
                'student_id' =>$student_ids[$i],
                'attendance_cycle_id' =>$attendance_cycle_id,
                'crouse_time_id' =>$crouse_time_id,
                'attendance_date' =>$attendance_date,
                'classroom_id' =>$classroom_id
            ];
        }
        $res = Student_attendance::insert($data);
        return Admin::content(function (Content $content) {
            $content->header(trans('admin.student_attendance'));
            $content->description(trans('admin.list'));
            $content->body($this->attendance_grid());
        });
    }
}
